In [2]:
import graphlab
In [4]:
sales = graphlab.SFrame('home_data.gl/')
In [5]:
sales
Out[5]:
The house price is correlated with the number of square feet of living space.
In [7]:
graphlab.canvas.set_target("ipynb")
sales.show(view="Scatter Plot", x="sqft_living", y="price")
Split data into training and testing.
.8 = 80% of the data is for training and other 20% for test.
We use seed=0 so that everyone running this notebook gets the same results. In practice, you may set a random seed (or let GraphLab Create pick a random seed for you).
In [8]:
train_data,test_data = sales.random_split(.8,seed=0)
In [9]:
sqft_model = graphlab.linear_regression.create(train_data, target='price', features=['sqft_living'])
In [10]:
print test_data['price'].mean()
evaluate() takes a test dataset and returns statistics about that set
In [11]:
print sqft_model.evaluate(test_data)
RMSE of about \$255,170!
Matplotlib is a Python plotting library that is also useful for plotting. You can install it with:
'pip install matplotlib'
In [13]:
import matplotlib.pyplot as plt
%matplotlib inline
In [17]:
# predict() outputs SArray column with predicted values
plt.plot(test_data['sqft_living'],test_data['price'],'+',
test_data['sqft_living'],sqft_model.predict(test_data),'-')
Out[17]:
Above: blue dots are original data, green line is the prediction from the simple regression.
Below: we can view the learned regression coefficients.
In [22]:
sqft_model.get('coefficients')
Out[22]:
In [23]:
my_features = ['bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'floors', 'zipcode']
In [24]:
sales[my_features].show()
In [25]:
sales.show(view='BoxWhisker Plot', x='zipcode', y='price')
Pull the bar at the bottom to view more of the data.
98039 is the most expensive zip code.
In [26]:
my_features_model = graphlab.linear_regression.create(train_data,target='price',features=my_features,validation_set=None)
In [27]:
print my_features
In [28]:
print sqft_model.evaluate(test_data)
print my_features_model.evaluate(test_data)
The RMSE goes down from \$255,170 to \$179,508 with more features.
The first house we will use is considered an "average" house in Seattle.
In [30]:
house1 = sales[sales['id']=='5309101200']
In [33]:
house1
Out[33]:
In [34]:
print house1['price']
In [35]:
print sqft_model.predict(house1)
In [37]:
print my_features_model.predict(house1)
In this case, the model with more features provides a worse prediction than the simpler model with only 1 feature. However, on average, the model with more features is better.
In [38]:
house2 = sales[sales['id']=='1925069082']
In [39]:
house2
Out[39]:
In [40]:
print sqft_model.predict(house2)
In [41]:
print my_features_model.predict(house2)
In this case, the model with more features provides a better prediction. This behavior is expected here, because this house is more differentiated by features that go beyond its square feet of living space, especially the fact that it's a waterfront house.
In [42]:
bill_gates = {'bedrooms':[8],
'bathrooms':[25],
'sqft_living':[50000],
'sqft_lot':[225000],
'floors':[4],
'zipcode':['98039'],
'condition':[10],
'grade':[10],
'waterfront':[1],
'view':[4],
'sqft_above':[37500],
'sqft_basement':[12500],
'yr_built':[1994],
'yr_renovated':[2010],
'lat':[47.627606],
'long':[-122.242054],
'sqft_living15':[5000],
'sqft_lot15':[40000]}
In [43]:
print my_features_model.predict(graphlab.SFrame(bill_gates))
The model predicts a price of over $13M for this house! But we expect the house to cost much more. (There are very few samples in the dataset of houses that are this fancy, so we don't expect the model to capture a perfect prediction here.)
In [50]:
highest_sales = sales[sales['zipcode']=='98039']
In [53]:
highest_sales_avg_price = highest_sales['price'].mean()
In [54]:
print highest_sales_avg_price
In [58]:
highest_sales_range = highest_sales[(highest_sales['sqft_living'] > 2000) & (highest_sales['sqft_living'] <= 4000)]
In [64]:
len(highest_sales_range) / float(50)
Out[64]:
In [65]:
advanced_features = [
'bedrooms', 'bathrooms', 'sqft_living', 'sqft_lot', 'floors', 'zipcode',
'condition',
'grade',
'waterfront',
'view',
'sqft_above',
'sqft_basement',
'yr_built',
'yr_renovated',
'lat', 'long',
'sqft_living15',
'sqft_lot15'
]
In [72]:
advanced_features_model = graphlab.linear_regression.create(train_data, target='price',features=advanced_features, validation_set=None)
In [73]:
my_features_model.evaluate(test_data)
Out[73]:
In [74]:
advanced_features_model.evaluate(test_data)
Out[74]:
In [ ]: